import { _decorator, Animation, Color, Component, Label, Material, Node, safeMeasureText, UITransform } from 'cc';
import { aa } from '../frame/FrameCore';
import configs from '../Configs';
const { ccclass, property, executeInEditMode } = _decorator;
const tempColor = new Color();
const colors = ["#94A5FF", "#7495FF", "#A3FF4E", "#B4CBF5"]
@ccclass('ChatBoxView')
export class ChatBoxView extends Component {
    @property(Material) mat: Material = null;
    @property(Label) nameLabel: Label;
    @property(Label) contentLabel: Label;
    private _wait: Node = null;
    private _transform: UITransform = null;
    /** 打字效果用的缓存 */
    private _fullContent: string = "";  // 当前要显示的完整内容
    private _displayedLength: number = 0; // 当前显示到哪了
    private _blank: string = "         : ";
    private _typingSpeed: number = 1; // 每帧显示几个字

    @property testLabel = "";

    @property
    set test(v) {
        this.createChat("喵喵", this.testLabel)
    }
    get test() {
        return false;
    }

    onLoad() {
        // this.font.node.on(Node.EventType.CHILD_ADDED,this.cb,this);
    }
    onEnable(): void {
        this._transform = this.node.getComponent(UITransform);
        // this.contentLabel.node.on(Node.EventType.SIZE_CHANGED,this.resize,this);
    }
    onDisable(): void {
        // this.contentLabel.node.off(Node.EventType.SIZE_CHANGED,this.resize,this);
        this.putWait();
        this.contentLabel.string = "";
        this.nameLabel.string = "";
    }
    onWait(name = configs.botName, index = 0) {
        this._wait = aa.res.get("ChatWaiting", this.contentLabel.node);
        this._wait.getComponent(Animation).play();
        this.nameLabel.color = Color.fromHEX(tempColor, colors[index]);
        this.nameLabel.string = name;
        this.contentLabel.string = "         : ";
        this._transform.setContentSize(202, 43.8);
        return this;
    }
    delayChat(name: string, content: string, index: number = 0, time = 1) {
        this.scheduleOnce(() => {
            this.createChat(name, content, index);
        }, time)

    }

    cb(node) {
        // const label:Label = node.getComponent(Label);
        // label.customMaterial = this.mat;
    }
    createAny(content: string) {
        this.contentLabel.string = content;
    }

    putWait() {
        if (this._wait) {
            this._wait.getComponent(Animation).stop();
            aa.res.put(this._wait);
            this._wait = null;
        }
    } /**
    * 流畅地显示内容，每帧追加 n 个字
    * @param content 最新的完整内容
    */
    public updateContent(content: string) {
        this.putWait();
        if (this._fullContent !== content) {
            // 只有新内容和上次缓存不一样才刷新
            this._fullContent = content;
            // 游标回退或前进，保证每次都是“打字效果”
            if (this._displayedLength > content.length) {
                this._displayedLength = 0;
            }
        }
        // 每次收到新内容后，自动开启 update 调度
        this.unschedule(this._typingUpdate);
        this.schedule(this._typingUpdate, 0); // 每帧执行
    }

    /** 内部 update 函数。每帧推进显示 */
    private _typingUpdate = () => {
        const fullLength = this._fullContent.length
        if (this._displayedLength < fullLength) {
            this._displayedLength += this._typingSpeed;
            if (this._displayedLength > fullLength) {
                this._displayedLength = fullLength;
            }
            const showStr = this._blank + this._fullContent.substring(0, this._displayedLength);
            this.contentLabel.string = showStr;
            // 宽度自适应
            const labelWidth = this._displayedLength * 24 + 140;
            this._transform.width = labelWidth > 900 ? 930 : labelWidth + 28;
        } else {
            // 全部显示完了，停止 update
            this.unschedule(this._typingUpdate);
        }
    }
    createChat(name: string, content: string, index: number = 0) {
        this.putWait();
        this.unschedule(this._typingUpdate);
        const col = colors[index];
        this.nameLabel.color = Color.fromHEX(tempColor, col);
        this.nameLabel.string = name;
        let blank = "         : ";
        this.contentLabel.string = blank + content;
        const labelWidth = content.length * 22 + 140;
        this._transform.width = labelWidth > 900 ? 930 : labelWidth + 28;
    }


}

